﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SDM.ExamPlanner.BLL;
using SDM.ExamPlanner.DTO;

namespace sdm.exam.ui.ClassAdministration.TeamRegistraion
{
    /// <summary>
    /// Interaction logic for edditTeamRegistration.xaml
    /// </summary>
    public partial class edditTeamRegistration : UserControl
    {
        private Dictionary<int, string> _schoolClassDictionary;
        private Dictionary<int, string> _educationDictionary;
 
        readonly Manager<SchoolClass> _schoolClassManager = new Manager<SchoolClass>();
        Manager<Education> _educationManager = new Manager<Education>();

        public edditTeamRegistration()
        {
            InitializeComponent();
            PopulateClassCbb();
        }

        private void PopulateClassCbb()
        {
            btnSubmit.IsEnabled = false;
            _schoolClassDictionary = _schoolClassManager.Find().Where(item => item.IsDeleted == false).ToDictionary(item => item.Id, item => item.Name);
            cbbClass.DataContext = _schoolClassDictionary.Values;
        }

        private void cbbClass_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _educationDictionary =
                _educationManager.Find().ToDictionary(item => item.Id, item => item.Name);            
            cbbEducation.DataContext = _educationDictionary.Values;
            
            btnSubmit.IsEnabled = true;
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {

            KeyValuePair<int, string> educationSelected = _educationDictionary.FirstOrDefault(c => c.Value.Equals(cbbEducation.SelectedValue));

            var selectedClass = cbbClass.SelectedValue.ToString();
            var selectedEducation = _educationDictionary.Keys.FirstOrDefault();
            var endDate = datePickerEnd.SelectedDate;
            var startDate = datePickerStart.SelectedDate;

            var editTeam = _schoolClassManager.Find().FirstOrDefault(item => item.Name == selectedClass);
            editTeam.Name = selectedClass;
            //editTeam.Education = selectedEducation;
            editTeam.EndDate = (DateTime) endDate;
            editTeam.StartDate=(DateTime) startDate;
            _schoolClassManager.Save(editTeam);

        }
    }
}
